<template>
    <div>
        <el-row>
            <el-col :span="24">
                <el-container>
<!--                    <el-header style="background: white">-->
<!--                        <span style="font-weight:bold;">聊天室成员:-->
<!--                            {{ user_list }}</span>-->
<!--                    </el-header>-->
                    <el-row>
                        <el-scrollbar id="chatRecord" ref="myScrollbar">
                            <div class="chat_back">
                                <div  v-for="item in chat_data">
                                    <div class="time">
                                        {{ item.time }}
                                    </div>
                                    <div v-if="item.chat_id === chat_id" class="text-wrapper">
                                        <span style="margin-left:10px;font-weight:bold;">{{ item.name }}:</span>
                                        <span class="info-content">{{ item.content }}</span>
                                    </div>
                                 <!--   <div class="message_time"> {{ item.time }}</div>-->
                                </div>
                            </div>
                        </el-scrollbar>
                        <div class="inputText">
                            <el-input class="inputText" type="textarea" :rows="5" v-model="content" style="width: 600px"></el-input>
                            <el-col class="buttonPart">
                                <el-button  :disabled="content == ''"style="float: right"  type="success" @click="send()">发  送</el-button>
                                <el-button type="primary"plain @click="openTip">使用技能</el-button>
<!--                                <el-button type="primary"plain>创建私聊</el-button>-->
<!--                                <el-button type="primary">创建聊天</el-button>-->
                            </el-col>
                        </div>
                    </el-row>

                </el-container>
            </el-col>
        </el-row>

    </div>
</template>

<script>
import { h } from 'vue';
export default {
    name: 'ChatRoom',
    props: ['chat_data', 'chat_id', 'c_name', 'level'],
    data() {
        return {
            user_list: ['a', 'b', 'c'],
            content: ''
            // id:"default"
        };
    },
    created() {
        // this.id=this.$route.query.id;
        console.log(this.chat_id)
    },
    methods: {
        send() {
            if(this.level===0){
                this.$message.error('观众无法发送消息')
                this.content=''
            }else{
                this.$emit('sendMes', { content:this.content,chat_id:this.chat_id });
                this.content = '';
            }
        },
        openTip(){
            // this.$msgbox({
            //     title: '消息',
            //     message: h('p', null, [
            //         h('span', null, '.rd +技能/属性'),
            //         h('span', null, '.r + 色子（1d4,3d6,1d100，等等，支持加减乘除）'),
            //         h('span', null, 'a d b 表示b面色子投a个'),
            //     ]),
            //     showCancelButton: true,
            //     confirmButtonText: '确定',
            // })
            this.$alert('.rd +技能/属性\n    ' +
                '.r + 色子（1d4,3d6,1d100，等等，支持加减乘除）\n    ' +
                'a d b 表示b面色子投a个'
                , '技能提示', {
                confirmButtonText: '确定',
            });
        },
        toBottom(){
            this.$nextTick(() => {
                this.$refs['myScrollbar'].wrap.scrollTop = this.$refs['myScrollbar'].wrap.scrollHeight;
            })

        },
    },
    watch:{
        chat_data:{
            handler (val, olVal) {
                // this.$refs['myScrollbar'].wrap.scrollTop = this.$refs['myScrollbar'].wrap.scrollHeight;
                this.toBottom()
            },
            deep: true
        },
    },
    updated(){
        // 聊天定位到底部
        let ele = document.getElementById('chatRecord');
        ele.scrollTop = ele.scrollHeight;
    },
};
</script>

<style>

.chat_back {
    height: 400px;
    background-color:#eef1f6;

}
.time {
    /*text-align: right;*/
    margin-top: 10px;
    text-align:center;
    font-size: 8px;
    color:gray;
}
.text-wrapper {
    word-break: break-all;
    word-wrap: break-word;
}
.info-content {
    word-break: break-all;
    max-width: 55%;
    display: inline-block;
    padding: 15px;
    font-size: 14px;
    position: relative;
    margin-top: 8px;
    background: white;
    border-radius: 8px;
}
.info-content::before {
    position: absolute;
    left: -7px;
    top: 8px;
    content: "";
    border-right: 10px solid white;
    border-top: 8px solid transparent;
    border-bottom: 8px solid transparent;
}
.message_time {
    font-size: 12px;
    color: rgba(51, 51, 51, 0.8);
    margin: 0;
    height: 20px;
    line-height: 20px;
    margin-top: -5px;
    margin-top: 5px;
}


.inputText{
    /*outline:none;*/
    border: 0;
}
.buttonPart{
    background-color: white;
}

</style>
